package org.nutboat.core;

import java.util.ArrayList;

public abstract class ListenerList<L extends GameListener, E extends GameEvent<?>> {
	private ArrayList<L> listeners;
	
	public ListenerList() {
		listeners = new ArrayList<L>();
	}
	
	public L getListener(int index) {
		return listeners.get(index);
	}
	
	public synchronized void addListener(L listener) {
		listeners.add(listener);
	}
	
	public synchronized boolean removeListener(L listener) {
		return listeners.remove(listener);
	}
	
	public int size() {
		return listeners.size();
	}
	
	public boolean isNotEmpty() {
		return listeners.size() != 0;
	}
	
	public void catchEvent(E event) {
		for (int i = listeners.size() - 1; i >= 0; --i) {
			handleEvent(listeners.get(i), event);					
		}
	}

	protected abstract void handleEvent(L listener, E event);
}